package com.bootcamp.javase2.reflection;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

/**
 * @program: day23ReflectionCode
 * @description:
 * @author: Keyu Li
 * @create: 2021-09-17 19:42
 **/

public class Work2 {

    public static void main(String[] args)
            throws NoSuchMethodException, IOException, InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException {
        callTargetMethod("./src/com/bootcamp/javase2/reflection/methodConfig.properties");
//        TestFieldUpdate obj = new TestFieldUpdate();

    }

    /**
     * @param configFilePath 表示配置文件的路径
     */
    public static void callTargetMethod(String configFilePath)
            throws IOException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException {
        // 新建 property 对象
        Properties properties = new Properties();
        // 加载输入流
        properties.load(new FileInputStream(configFilePath));
        // 获取指定 className, methodName
        String className = properties.getProperty("className");
        String methodName = properties.getProperty("methodName");
        // 新建 Class对象
        Class<?> aClass = Class.forName(className);
        // 获取指定 method
        Method method = aClass.getMethod(methodName);
        System.out.println(method.getModifiers());
        // 调用方法
        method.setAccessible(true);
        method.invoke(aClass.newInstance());
    }
}

class TestMethodInvoke {
    public TestMethodInvoke() {
    }

    public final void eatFinalApple(){
        System.out.println("public final eat apple");
    }

    public void eatApple(){
        System.out.println("public eat Apple!!!");
    }
    private void eatBanana(){
        System.out.println("private eat banana!!!");
    }
}